package com.xintonghua.bussiness.ui.fragment.client.user;

import android.app.Activity;
import android.content.Intent;
import android.graphics.MaskFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.GoodsManageBean;
import com.xintonghua.bussiness.ui.view.GoodsCounter;
import com.xintonghua.bussiness.ui.view.OnBuyNumChangedListener;
import com.xintonghua.bussiness.util.AbstractBaseAdapter;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MathUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;
import com.xintonghua.bussiness.util.ViewHolder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by liqi on 2018/3/31.
 * 添加产品
 */
public class ProductAddActivity extends BaseActivity implements PriceCalculate {
    @BindView(R.id.tv_jinger)
    TextView tvJinger;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.tv_search_product)
    TextView tvSearchProduct;
    @BindView(R.id.lv_list)
    ListView lvList;
    List<GoodsManageBean> goodsManageBeanList = new ArrayList<>();

    Contant contant;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_product);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("添加产品");
        contant = (Contant) JumpUtils.getSerializable(this);
        initUI();
    }

    BaseAdapter adapter;

    @Override
    public void initUI() {
        super.initUI();
        lvList.setAdapter(adapter = new AbstractBaseAdapter<GoodsManageBean>(goodsManageBeanList) {
            @Override
            public View getAdapterViewAtPosition(final int position, View convertView, ViewGroup parent) {

                if (convertView == null) {
                    convertView = LayoutInflater.from(ProductAddActivity.this).inflate(R.layout.adapter_searchproduct_add_or_jian, null);
                }
                final GoodsManageBean bean = getItem(position);
                ImageView ivGoodsHead = ViewHolder.get(convertView, R.id.iv_goods_head);
                TextView tv_goods_name = ViewHolder.get(convertView, R.id.tv_goods_name);
                TextView tv_goods_price = ViewHolder.get(convertView, R.id.tv_goods_price);
                TextView tv_goods_count = ViewHolder.get(convertView, R.id.tv_goods_count);
                RelativeLayout rlGoods = ViewHolder.get(convertView, R.id.rl_goods);
                GoodsCounter counter = ViewHolder.get(convertView, R.id.goods_counter);
                counter.setMax(bean.getInventory());
                counter.setMin(1);
                counter.setOnNumberChangerClistener(new OnBuyNumChangedListener() {
                    @Override
                    public void onBuyNumChanged(String num) {
                        goodsManageBeanList.get(position).setShangPingNumber(Integer.parseInt(num));
                        notifyPrice();
                    }
                });

                GlideUtils.load(ProductAddActivity.this, ivGoodsHead, bean.getImgs());
                tv_goods_name.setText(bean.getName());
                tv_goods_price.setText(MyUtils.getMoney(ProductAddActivity.this, bean.getPrice()));
                tv_goods_count.setVisibility(isCheck() ? View.GONE : View.VISIBLE);
                tv_goods_count.setText("剩余库存" + bean.getInventory() + "件");
                return convertView;
            }

        });
    }

    private boolean check = false;

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestTwo:
                    mToast("操作成功");
                    finish();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.btn_ok, R.id.tv_search_product})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                buy();
                break;
            case R.id.tv_search_product:
                JumpUtils.jumpActivityForResult(ProductAddActivity.this, SearchProductActivity.class, null, MyState.requestOne);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MyState.requestOne && resultCode == Activity.RESULT_OK) {

            if (data != null) {
                GoodsManageBean bean = data.getParcelableExtra("data");
                if (bean != null) {
                    tvSearchProduct.setVisibility(View.GONE);
                    goodsManageBeanList.add(bean);
                    notifyPrice();
                    adapter.notifyDataSetChanged();
                }

            }
        }
    }

    @Override
    public void notifyPrice() {
        if (!MyUtils.isNullOrEmpty(goodsManageBeanList)) {
            String money = "0";
            for (int i = 0; i < goodsManageBeanList.size(); i++) {
                String a1 = MathUtils.strMul2("" + goodsManageBeanList.get(i).getPrice(), "" + goodsManageBeanList.get(i).getShangPingNumber(), 2);
                money = MathUtils.strAdd(money, a1, 2);

            }
            tvJinger.setText("¥" + money);
        }
    }

    @Override
    public void buy() {

        if (MyUtils.isNullOrEmpty(goodsManageBeanList)) {
            MyUtils.mToast(ProductAddActivity.this, "请选择要添加的产品!");
            return;
        }
        httpCent.buyShopProduct("" + contant.getId(), "" + goodsManageBeanList.get(0).getId(), "" + goodsManageBeanList.get(0).getShangPingNumber(), this, MyState.requestTwo);
    }

}


interface PriceCalculate {
    void notifyPrice();//更新价格

    void buy();//客户购买商品
}

